<template>
    <el-breadcrumb class="app-breadcrumb" separator="/">
        <transition-group name="breadcrumb">
            <el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="item.path">
        <span v-if="item.redirect === 'noredirect' || index === breadcrumbs.length-1"
              class="no-redirect">{{ item.meta.title }}</span>
                <a v-else @click.prevent="handleLink(item)">{{item.meta.title }}</a>
            </el-breadcrumb-item>
        </transition-group>
    </el-breadcrumb>
</template>

<script lang="ts">
    import pathToRegexp from 'path-to-regexp'
    import {Component, Vue, Watch} from 'vue-property-decorator'
    import {Route, RouteRecord} from 'vue-router'

    @Component({
        name: 'Breadcrumb'
    })
    export default class extends Vue {
        private breadcrumbs: RouteRecord[] = [];

        //监听路由变化
        @Watch('$route')
        private onRouteChange(route: Route) {
            if (route.path.startsWith('/redirect/')) {
                return
            }
            this.getBreadcrumb()
        }

        created() {
            this.getBreadcrumb()
        }

        //刷新显示路径
        private getBreadcrumb() {
            // //////console.log("this.$route.matched.size", this.$route.matched.length);
            // //////console.log("this.$route.matched", this.$route.matched);

            //获取title不为null的路由
            let matched = this.$route.matched.filter((item) => item.meta && item.meta.title && item.meta.title != "");

            //拿到第一个路由
            const firstRouter = matched[0];
            // //////console.log("firstRouter", firstRouter);

            //是首页只展示首页
            let isIndex = this.isIndex(firstRouter);
            if (isIndex) {
                this.breadcrumbs = [{path: '/index', meta: {title: '首页'}} as RouteRecord];
            }

            //不是首页，把首页插入到数组第一位
            if (!isIndex) {
                matched = [{path: '/index', meta: {title: '首页'}} as RouteRecord].concat(matched)
                this.breadcrumbs = matched.filter((item) => {
                    // //////console.log("item", item);
                    return item.meta && item.meta.title && item.meta.breadcrumb !== false
                });
            }

        }

        private isIndex(route: RouteRecord) {
            return route && route.name && route.name.trim().toLocaleLowerCase() === 'Index'.toLocaleLowerCase()
        }

        private pathCompile(path: string) {
            const {params} = this.$route;
            const toPath = pathToRegexp.compile(path);
            return toPath(params)
        }

        private handleLink(item: any) {
            const {redirect, path} = item;
            if (redirect) {
                this.$router.push(redirect);
                return
            }
            this.$router.push(this.pathCompile(path))
        }
    }
</script>

<style lang="scss" scoped>
    .el-breadcrumb__inner,
    .el-breadcrumb__inner a {
        font-weight: 400 !important;
    }

    .app-breadcrumb.el-breadcrumb {
        display: inline-block;
        font-size: 14px;
        line-height: 50px;
        margin-left: 8px;

        .no-redirect {
            color: #97a8be;
            cursor: text;
        }
    }
</style>
